package com.example.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.book.entity.Products;
import com.example.book.entity.User;
import com.example.book.service.OrdersService;
import com.example.book.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ryl
 * @since 2021-05-05
 */
@RestController
@RequestMapping("/book/orders")
public class OrdersController {


    @Autowired
    ProductsService productsService;

    @RequestMapping("/addCart")
    private Map<String,Object> addCart(HttpServletRequest request,Integer bookId, String userName){
        Map<String,Object> map=new HashMap<>();
        if(userName.equals("")||userName==null){
            map.put("flag",false);
            return map;
        }
        QueryWrapper<Products> queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",bookId);
        List<Products> list = productsService.list(queryWrapper);
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(userName + "_cart");

        if(attribute==null){
            List<Products> carts=new ArrayList<>();
            session.setAttribute(userName+"_cart",carts);
        }

        List<Products> carts=(List<Products>) session.getAttribute(userName + "_cart");
        addCartCount(carts,list.get(0));
        calPriceAndCount(userName,carts,session);
        map.put("flag",true);
        map.put("msg",list.get(0).getName());
        return map;
    }

    private void addCartCount(List<Products> carts,Products cart){
        if(carts.contains(cart)){
            for(Products products:carts){
                if(products.getName().equals(cart.getName())){
                    products.setCount(products.getCount()+1);
                }
            }
        }
        else{
            carts.add(cart);
        }
    }

    @RequestMapping("/deleteCartBook")
    private void deleteCartBook(String name,String username,HttpServletRequest request){
        HttpSession session = request.getSession();
        Object attribute = session.getAttribute(username + "_cart");
        if(attribute==null){
            return;
        }
        List<Products> carts=(List<Products>) attribute;
        Iterator<Products> iterable=carts.iterator();
        while (iterable.hasNext()){
            Products products = iterable.next();
            if(products.getName().equals(name)){
                if(products.getCount()==1) iterable.remove();
                else products.setCount(products.getCount()-1);
                break;
            }
        }
        calPriceAndCount(username,carts,session);

    }

    private void calPriceAndCount(String userName,List<Products> carts,HttpSession session){
        double totalCount=0;
        double totalPrice=0;
        for(Products product:carts){
            totalCount+=product.getCount();
            totalPrice+=product.getCount()*product.getPrice();
        }
        session.setAttribute(userName+"_cart_totalCount",totalCount);
        session.setAttribute(userName+"_cart_totalPrice",totalPrice);
    }
}

